package com.llly.controller;

import com.llly.client.NewsFeignClient;
import com.llly.client.ProductFeignClient;
import com.llly.client.ShoppingCarFeignClient;
import com.llly.client.UserFeignClient;
import com.llly.pojo.Easybuy_product_category;
import com.llly.pojo.Easybuy_user;
import com.llly.pojo.ShoppingCar;
import com.llly.util.Md5Utils;
import com.llly.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private NewsFeignClient newsFeignClient;

    @Autowired
    private ShoppingCarFeignClient shoppingCarFeignClient;

    @RequestMapping("/gologin")
    public ModelAndView gologin(ModelAndView mv){
        String codeImage = VerifyCodeUtils.createOneCodeImage();
        mv.addObject("codeImage",codeImage);
        mv.setViewName("Login");
        return mv;
    }

    @RequestMapping("/goRegist")
    public ModelAndView goRegist(ModelAndView mv){
        String codeImage = VerifyCodeUtils.createOneCodeImage();
        mv.addObject("codeImage",codeImage);
        mv.setViewName("Regist");
        return mv;
    }

    //登录验证1
    @ResponseBody
    @RequestMapping("/login.do")
    public boolean Userclient(ModelAndView mv,String loginName, String password, HttpSession session){
        boolean flag=false;
        Easybuy_user user = userFeignClient.login(loginName, password);
        if (user!=null){
            flag=true;
        }else{
            flag=false;
        }
        return flag;
    }

    //登录
    @RequestMapping("/login")
    public ModelAndView loginnn(ModelAndView mv,String loginName, String password, HttpSession session){
        //password=( Md5Utils.getMd5Code(password));
            System.out.println("用户名"+loginName+"pwd:"+password);
        Easybuy_user user = userFeignClient.login(loginName, password);
        if (user==null){
            mv.setViewName("redirect:/gologin");
        }else{
            session.setAttribute("user",user);
            mv.setViewName("forward:/indexShow");
        }
        return mv;
    }

    @ResponseBody
    @RequestMapping("/indexShow")   //首页显示
    public ModelAndView indexShow(ModelAndView mv,HttpSession session){
        System.out.println("8081首页方法");
        List<Easybuy_product_category> allEpcs = productFeignClient.findAllEpcs();   //左侧分类栏及商品
        Map<String, Object> map1 = newsFeignClient.ShowNews(10);//右侧新闻栏

        //购物车显示
        Easybuy_user user = (Easybuy_user) session.getAttribute("user");
        Integer uid = user.getId();
        Map<String, Object> map = shoppingCarFeignClient.showAllShoppingCar(uid);
        if (map!=null){
            List<ShoppingCar> shoppingCars= (List<ShoppingCar>) map.get("cars");
            mv.addObject("snum",shoppingCars.size());
            mv.addObject("zj",map.get("zj"));
            mv.addObject("shopcars",shoppingCars);
        }else{
            mv.addObject("snum",0);
            mv.addObject("zj",0);
        }
        mv.addObject("news",map1.get("pg"));
        mv.addObject("epcs",allEpcs);   //1级分类
        mv.setViewName("Index");
        return mv;
    }

    @ResponseBody
    @RequestMapping("/reg")
    public boolean reg(String loginName){
        boolean flag=false;
        Easybuy_user easybuy_user = userFeignClient.login(loginName, "");
        if (easybuy_user==null){
            flag=true;
        }
        return flag;
    }

    //注册，新增用户
    @RequestMapping("/adduser")
    public ModelAndView adduser(ModelAndView mv,Easybuy_user ea){
        //md5,加密，在查询
        //ea.setPassword( Md5Utils.getMd5Code(ea.getPassword()));
        Easybuy_user login = userFeignClient.login(ea.getLoginName(),"");
        if(login==null||login.equals("")){
            int adduser = userFeignClient.adduser(ea);
            mv.setViewName("redirect:/gologin");
        }else {
            mv.setViewName("redirect:/goRegist");
        }
        return mv;
    }
    //管理员，id查询，查看用户详情
    @RequestMapping("/iddetails")
    public ModelAndView iddetails(ModelAndView mv,Integer id){
        Easybuy_user iddetails = userFeignClient.iddetails(id);
        mv.addObject("iddetails",iddetails);
        mv.setViewName("Manager/index");
        return mv;
    }
    //管理员，查看用户
    @RequestMapping("/showUsers")
    public ModelAndView showUsers(ModelAndView mv,Integer PageIndex){
        AllUsers(mv,PageIndex);
        mv.setViewName("Manager/user");
        return mv;
    }
    //管理员，id查询，修改回显
    @RequestMapping("/iddetails2")
    public ModelAndView iddetails2(ModelAndView mv,Integer id){
        Easybuy_user iddetails = userFeignClient.iddetails(id);
        mv.addObject("iddetails",iddetails);
        mv.setViewName("Manager/user-modify");
        return mv;
    }
    //管理页面，修改
    @PostMapping("/updateUserss")
    public ModelAndView updateUser(ModelAndView mv,Easybuy_user ea,Integer PageIndex){
        int i = userFeignClient.updateUser(ea);
        if(i>0){
            AllUsers(mv,PageIndex);
            mv.setViewName("Manager/user");
        }else {
            mv.setViewName("Manager/user");
        }
        return mv;
    }

    @RequestMapping("/gouseradd")
    public ModelAndView gouseradd(ModelAndView mv){
        mv.setViewName("Manager/user-add");
        return mv;
    }

    //管理者页面，新增用户
    @PostMapping("/addusers2")
    public ModelAndView addusers(ModelAndView mv,Easybuy_user ea,Integer PageIndex){
        int adduser = userFeignClient.adduser(ea);
        if(adduser>0){
            AllUsers(mv,PageIndex);
            mv.setViewName("Manager/user");
        }else {
            mv.setViewName("Manager/user");
        }
        return mv;
    }
    //管理者页面，删除用户信息
    @ResponseBody
    @RequestMapping("/deleteUsers")
    public ModelAndView deleteUsers(Integer id,ModelAndView mv,Integer PageIndex){
        System.out.println("id"+id);
        int i = userFeignClient.deleteUsers(id);
        System.out.println("IIIII"+i);
        if(i>0){
            AllUsers(mv,PageIndex);
            mv.setViewName("Manager/user");
        }else {
            mv.setViewName("Manager/user");
        }
        return mv;
    }


    @RequestMapping("/userInfo")
    public ModelAndView userInfo(ModelAndView mv){
        mv.setViewName("UserInfo");
        return mv;
    }

    @RequestMapping("/goindex")
    public ModelAndView goindex(ModelAndView mv){
        mv.setViewName("Manager/index");
        return mv;
    }



    @RequestMapping("/exit.action")    //退出
    public ModelAndView exit(HttpSession session,ModelAndView mv){
        session.removeAttribute("user");
        session.invalidate();
        mv.setViewName("forward:/gologin");
        return mv;

        //return "redirect:/Login.html";
    }

    //带分页的查询用户信息
    public ModelAndView AllUsers(ModelAndView mv,Integer PageIndex) {
        Map<String, Object> stringObjectMap = userFeignClient.showUsers(PageIndex == null ? 1 : PageIndex);
        List<Map<String, Object>> result = (List<Map<String, Object>>) stringObjectMap.get("Result");
        mv.addObject("easybuy_users", result);
        mv.addObject("Total", stringObjectMap.get("Total"));//总记录数
        mv.addObject("PageNum", stringObjectMap.get("PageNum"));//第几页
        mv.addObject("Pages", stringObjectMap.get("Pages"));//总页数
        Integer pages = (Integer) stringObjectMap.get("Pages");
        List pag = new ArrayList();
        for (int i = 1; i <= pages; i++) {
            pag.add(i);
        }
        mv.addObject("listPage", pag);
        return mv;
    }
}
